import { NPageHeader } from "naive-ui";
import type { BaseComponentType } from "../types";
import type { PageHeaderProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
export type PageHeaderFactoryConstructorType = {
  attrs?: HTMLAttributes;
  defaultSlot?: BaseComponentType;
  props?: PageHeaderProps;
  avatarSlot?: BaseComponentType;
  headerSlot?: BaseComponentType;
  extraSlot?: BaseComponentType;
  footerSlot?: BaseComponentType;
  titleSlot?: BaseComponentType;
  subtitleSlot?: BaseComponentType;
  backSlot?: BaseComponentType;
};
export class PageHeaderFactory {
  private attrs: HTMLAttributes = {};
  private props: PageHeaderProps = {};
  private defaultSlot: BaseComponentType = null;
  private avatarSlot: BaseComponentType = null;
  private headerSlot: BaseComponentType = null;
  private extraSlot: BaseComponentType = null;
  private footerSlot: BaseComponentType = null;
  private titleSlot: BaseComponentType = null;
  private subtitleSlot: BaseComponentType = null;
  private backSlot: BaseComponentType = null;

  constructor(data?: PageHeaderFactoryConstructorType) {
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
    if (data?.props) this.setProps(data.props);
    if (data?.avatarSlot) this.setAvatar(data.avatarSlot);
    if (data?.headerSlot) this.setHeader(data.headerSlot);
    if (data?.extraSlot) this.setExtra(data.extraSlot);
    if (data?.footerSlot) this.setFooter(data.footerSlot);
    if (data?.titleSlot) this.setTitle(data.titleSlot);
    if (data?.subtitleSlot) this.setSubtitle(data.subtitleSlot);
    if (data?.backSlot) this.setBack(data.backSlot);
  }
  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }
  setDefault(component: BaseComponentType) {
    this.defaultSlot = component;
    return this;
  }
  setProps(props: PageHeaderProps) {
    this.props = props;
    return this;
  }
  setHeader(component: BaseComponentType) {
    this.headerSlot = component;
    return this;
  }
  setAvatar(component: BaseComponentType) {
    this.avatarSlot = component;
    return this;
  }
  setExtra(component: BaseComponentType) {
    this.extraSlot = component;
    return this;
  }
  setFooter(component: BaseComponentType) {
    this.footerSlot = component;
    return this;
  }
  setTitle(component: BaseComponentType) {
    this.titleSlot = component;
    return this;
  }
  setSubtitle(component: BaseComponentType) {
    this.subtitleSlot = component;
    return this;
  }
  setBack(component: BaseComponentType) {
    this.backSlot = component;
    return this;
  }

  create() {
    return (
      <NPageHeader {...this.attrs} {...this.props}>
        {{
          default: () => getSlotsDom(this.defaultSlot),
          avatar: () => getSlotsDom(this.avatarSlot),
          header: () => getSlotsDom(this.headerSlot),
          extra: () => getSlotsDom(this.extraSlot),
          footer: () => getSlotsDom(this.footerSlot),
          title: () => getSlotsDom(this.titleSlot),
          subtitle: () => getSlotsDom(this.subtitleSlot),
          back: () => getSlotsDom(this.backSlot),
        }}
      </NPageHeader>
    );
  }
}
